<script lang="ts" setup>
import { useForm } from '@/hooks/web/useForm'
import { FormSchema } from '@/types/form'
import * as SkuApi from '@/api/client-mall/product/sku'

const message = useMessage()
const { t } = useI18n()

const dialogVisible = ref(false)
const dialogTitle = ref('新增课程')
const formLoading = ref(false)
const formType = ref<'create' | 'update'>('create')

const schema = reactive<FormSchema[]>([
  {
    field: 'spuId',
    label: '所属班型',
    value: '',
    component: 'Input',
    formItemProps: {
      rules: [{ required: true, message: '请选择所属班型' }]
    }
  },
  {
    field: 'payNote',
    label: '付款通知信息',
    value: '',
    component: 'Input',
    formItemProps: {
      rules: [{ required: true, message: '请输入付款通知信息' }]
    }
  },
  {
    field: 'skuName',
    label: '套餐名称',
    value: '',
    component: 'Input',
    formItemProps: {
      rules: [{ required: true, message: '请输入套餐名称' }]
    }
  },
  {
    field: 'picUrl',
    label: '套餐图片',
    value: '',
    component: 'UploadImg',
    formItemProps: {
      rules: [{ required: true, message: '请上传图片' }]
    }
  },
  {
    field: 'price',
    label: '套餐价格(元)',
    value: 0,
    component: 'InputNumber',
    formItemProps: {
      rules: [{ required: true, message: '请输入课程价格' }]
    }
  },
  {
    field: 'stock',
    label: '库存',
    value: 0,
    component: 'InputNumber',
    formItemProps: {
      rules: [{ required: true, message: '请输入课程库存' }]
    }
  },
  {
    field: 'warnStockLimit',
    label: '库存预警值',
    value: 0,
    component: 'InputNumber',
    formItemProps: {
      rules: [{ required: true, message: '请输入库存预警值' }]
    }
  },
  {
    field: 'status',
    label: '状态',
    value: 1,
    component: 'Radio',
    componentProps: {
      options: [
        { label: '上架', value: 1 },
        { label: '下架', value: 2 }
      ]
    }
  }
])

const { register, elFormRef, methods: formMethods } = useForm()

const open = (type: 'create' | 'update', spuId?: string, id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'update' ? '编辑课程' : '新增课程'
  formType.value = type

  if (type === 'create' && spuId) {
    formMethods.setValues({ spuId })
  }

  if (type === 'update') {
    formLoading.value = true
    SkuApi.get(id!).then((data) => {
      data.price = data.price / 100
      formMethods.setValues(data)
      console.log('测试11111111111', data)
      formLoading.value = false
    })
  }
}
defineExpose({ open })

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!elFormRef.value) return
  const valid = await elFormRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  try {
    const formData = (await formMethods.getFormData()) || {}

    if (formType.value === 'create') {
      // 转换表单
      const data = { ...(formData as any), price: formData.price * 100 }
      console.log('转换表单', data)
      await SkuApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      // 转换表单
      const data = { ...(formData as any), price: formData.price * 100 }
      console.log('111', data)
      await SkuApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="700">
    <Form :schema="schema" @register="register" />
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
